// pages/detail/detail.js
const mta = require('../../../mta_sdk/mta_analysis.js');
var ctx = null;
const app = getApp();
var pid = ''
Page({

  data: {
    base64: '',
    shared:0,
    txt: '',
    imgUrl:'',
    shareImg:'',
    ajaxSuccess:false
  },

  onLoad: function (opt) {
    mta.Page.init();
    console.log(opt);
    if (opt.imgUrl && opt.txt) {

      this.makeImg(opt.txt, opt.imgUrl)
      this.setData({
        txt: opt.txt,
        imgUrl: opt.imgUrl
      })
    }
    if (opt.shared == 1) {
      this.setData({
        shared: 1,
        shareImg: opt.shareImg,
        ajaxSuccess: true
      });

    }
    pid = opt.pid
  },

  makeImg(txt, imgUrl) {
    var me = this;
    if (me.data.shared == 1) {
      return false;
    }
    wx.showLoading({
      title: '加载中...',
    })
    var sysInfo = wx.getSystemInfoSync();
    var ft = sysInfo.windowWidth * .042;
    ctx = wx.createCanvasContext("mycanvas");
    var query = wx.createSelectorQuery();
    
    
    query.select('#canvas').boundingClientRect();//获取canvas的元素信息
    query.exec(res => {
      var w = res[0].width;
      var h = res[0].height
     

          ctx.drawImage(imgUrl, 0, 0, w, h);
          
          ctx.setFontSize(ft);
          ctx.fillStyle = 'red';
          
          ctx.fillText(txt, w / (10 + txt.length*2), h / 4.8);
          ctx.fillText(txt, w / (2+ txt.length*2), h / 4);
          ctx.draw(false, function () {
            app.buldingImg("mycanvas", callBack => {
              me.setData({
                shareImg: callBack.data.url,
                ajaxSuccess: true
              });
              wx.hideLoading();
            });
          });

    })

  },
  //保存图片
  //保存图片
  saveImg() {
    app.savePic("mycanvas");
  },
  onShareAppMessage: function () {
    return {
      title: '家长通知书制作',
      desc: '家长通知书制作',
      path: `/pages/${pid}/${pid}_detail/${pid}_detail?shared=1&shareImg=${this.data.shareImg}`,
      imageUrl: this.data.shareImg,
    }
  },
  toPlay() {
    wx.navigateTo({
      url: '/pages/tz/tz_make/tz_make?pid=tz',
    })
  },

  toHome() {
    wx.navigateTo({
      url: '/pages/index/index',
    })
  },
})